package org.lb.MarketingSystem.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.lb.MarketingSystem.entity.ProductType;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lb.MarketingSystem.entity.CustomerUser;
import org.lb.MarketingSystem.entity.Product;
import org.lb.MarketingSystem.service.CustomerUserService;
import org.lb.MarketingSystem.service.ProductService;
import org.lb.MarketingSystem.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用户功能控制controller
 * @author x
 *
 */
@Controller
@RequestMapping()
public class MartetingCustomerController {
	static int TypeID;
	static int productPage=1;
	static String UserName;
	static int CountPage;
	static CustomerUser reGcustomerUser;
	@Autowired
	private CustomerUserService customerService;
	@Autowired
	private ProductTypeService productTypeServiceImpl;
	@Autowired
	private ProductService productServiceImpl;
	
	@RequestMapping(value="/customer_login")
	public String customerCheckLogin(Model model){
		List<ProductType> pts=new ArrayList<ProductType>();
		pts=productTypeServiceImpl.getallProductType();
		model.addAttribute("pts",pts);
		return "index";
	}
	
	@RequestMapping(value="/customerLogin")
	public String customerLogin(String customerName,String password,Model model,HttpServletRequest request,HttpServletResponse response){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("customerName", customerName);
		map.put("password", password);
		CustomerUser customerUser = customerService.getConstomerUserByName(map);
		if (customerUser!=null) {
			request.getSession().setAttribute("customerUser", customerUser);
			model.addAttribute("customerUser", customerUser);
		}
		List<ProductType> pts=new ArrayList<ProductType>();
		pts=productTypeServiceImpl.getallProductType();
		model.addAttribute("pts",pts);
		return "index";
	}
	@RequestMapping(value="/productshow")
	public String productShow(@RequestParam(value="typeid") int typeid,Model model){
		CustomerUser cu=new CustomerUser();
		if(UserName.equals("用户登录"))
		{
			cu.setName("您好，请登录");
		}
		else
		{
			String[] userN=UserName.split("-");
			cu.setName("欢迎，"+userN[0]);
		}
		int Count=0;
		List<Product> products=productServiceImpl.getproductByproductTypeID(typeid);
		List<Product> productCount=productServiceImpl.getproductCountByType(typeid);
		if(productCount.size()%8==0)
		{
			Count=	productCount.size()/8;
		}
		else
		{
			Count=	productCount.size()/8+1;	
		}
		CountPage=Count;
		model.addAttribute("products",products);
		model.addAttribute("count",Count);
		model.addAttribute("Log",cu);
		return "productshow";
	}
	@RequestMapping(value="/userLogproductshow")
	public String userLoproductShow(@RequestParam(value="userName") String userName,@RequestParam(value="userPassword") String userPassword,Model model){
        CustomerUser cu=new CustomerUser();
        List<CustomerUser> CU=customerService.get_AllCustomerUsers();
        for(CustomerUser C:CU)
        {
        	if(userName.equals(C.getName()))
        	{
        		if(userPassword.equals(C.getPassword()))
        		{
        			cu.setName("欢迎，"+C.getName());
        		}
        	}
        }
		List<Product> products=new ArrayList<Product>();
		products=productServiceImpl.getproductByPage(TypeID, productPage);
		model.addAttribute("Log",cu);	
		model.addAttribute("products",products);
		model.addAttribute("count",CountPage);
		return "productshow";
	}

@RequestMapping(value="/prductajax")
@ResponseBody
public int sedTypeid(@RequestParam("typeid")int typeid,@RequestParam(value="user") String user){
	TypeID=typeid;
	UserName=user;
	return typeid;
}

@RequestMapping(value="/home")
public String homeShow(){
	return "home";
}


@RequestMapping(value="/userInfoAjax")
@ResponseBody
public String senduserName(@RequestParam(value="username") String username){
	UserName=username;
	return username;
}
@RequestMapping(value="/currentPageAjax")
@ResponseBody
public List<Product> getClassRoomByPage(@RequestParam("pagecount")int page,@RequestParam("typeID")int productTypeID)
{
	productPage=page;
	List<Product> products=new ArrayList<Product>();
	products=productServiceImpl.getproductByPage(productTypeID, page);
	return products;
}


@RequestMapping(value="/userInsertInfoAjax")
@ResponseBody
public int senduserInfo(@RequestParam(value="username") String username,@RequestParam(value="userpass") String userpass,@RequestParam(value="usersex") String usersex,@RequestParam(value="usercard") String usercard,@RequestParam(value="userage") int userage/*,@RequestParam(value="useradress") String useradress,@RequestParam(value="useremil") String useremil,@RequestParam(value="userphone") String userphone*/){
	System.out.println(userage);
	CustomerUser cu=new CustomerUser();
	cu.setName(username);
	cu.setPassword(userpass);
	cu.setAge(userage);
	/*reGcustomerUser.setAddress(useradress);
	reGcustomerUser.setEmil(useremil);
	reGcustomerUser.setPhoneNumber(userphone);*/
	cu.setSex(usersex);
	cu.setCard(usercard);
	reGcustomerUser=cu;
	customerService.addCustomerUser(MartetingCustomerController.reGcustomerUser);
	return 0;
}
}
